package com.F04.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.F04.actionForm.F0405ActionForm;
import com.F04.dao.F0405Dao;
import com.action.BaseAction;
import com.config.Constants;
import com.exception.SelectException;
import com.util.CommonTools;
import com.util.SessionUtils;

public class F0405Action extends BaseAction {
	Logger log = Logger.getLogger(F0405Action.class);

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		F0405ActionForm actionForm = (F0405ActionForm) form;
		F0405Dao dao = new F0405Dao();
		String operationType = actionForm.getOperationType();
		String language = SessionUtils.getLocale(request);
		actionForm.setLanguage(language);

		try {

			/*--------------------------------------------主頁面操作begin--------------------------------------------------*/

			// 初始化
			if (Constants.INIT.equals(operationType)) {
				this.doINIT(actionForm);
				return mapping.findForward(Constants.List);
			}

			// 检索
			if (Constants.QUERYLIST.equals(operationType)) {
				List list = dao.select(actionForm);
				if (list != null) {
					actionForm.setList(list);
				} else {
					actionForm.setList(new ArrayList());
					actionForm.setErrorFlag("2");
				}
				return mapping.findForward(Constants.List);
			}

			// 删除
			if (Constants.DELETE.equals(operationType)) {
				dao.delete(actionForm);
				return mapping.findForward("operateReturnList");
			}

			// 新增初始化
			if (Constants.ADD.equals(operationType)) {
				this.initActionForm(actionForm);
				return mapping.findForward(Constants.QUERYDETAIL);
			}

			// 编辑初始化
			if (Constants.EDIT.equals(operationType)) {
				dao.selectByCode(actionForm);
				return mapping.findForward(Constants.EDIT);
			}

			/*--------------------------------------------主頁面操作end--------------------------------------------------*/

			/*--------------------------------------------明細頁面操作begin--------------------------------------------------*/

			// 新增保存
			if (Constants.SAVE_INSERT.equals(operationType)) {
				String id = CommonTools.null2String(actionForm.getId());
				String[] materialCode = request
						.getParameterValues("material_code");
				Map map = new HashMap();
				if (id != null && !"".equals(id)) {
					int i = 0;
					log.info(String.valueOf(id.split(",").length));
					for (i = 0; i < id.split(",").length; i++) {
						log.info(materialCode[i]);
						map.put("materialCode" + i, materialCode[i]);
					}
					dao.insert(actionForm, map, i);
				}
				return mapping.findForward("operateReturnList");
			}

			// 编辑保存
			if (Constants.SAVE_UPDATE.equals(operationType)) {
				dao.update(actionForm);
				return mapping.findForward("operateReturnList");
			}

			// 回显(新增和編輯保存后，返回主頁面)
			if ("operateReturnList".equals(operationType)) {
				actionForm.setMaterialName("");
				actionForm.setSupplierName("");
				List list = dao.select(actionForm);
				if (list != null) {
					actionForm.setList(list);
				} else {
					actionForm.setList(new ArrayList());
				}
				this.initActionForm(actionForm);
				actionForm.setErrorFlag("0");
				return mapping.findForward(Constants.List);
			}
			
//			明细页面返回主页面
			if("detailBack".equals(operationType)){
				this.initActionForm(actionForm);
				actionForm.setErrorFlag("");
				return mapping.findForward(Constants.List);
			}

			// 动态获取供应商的名称和编码
			if (Constants.AJAX.equals(operationType)) {
				this.initList( dao, request, response);
				// actionForm.setListSupplier(dao.initSupplier(actionForm));
				return null;
			}
			/*--------------------------------------------明細頁面操作end--------------------------------------------------*/
		} catch (Exception e) {
			e.printStackTrace();
			this.initActionForm(actionForm);
			actionForm.setErrorFlag("3");
			dao.rollback();
			return mapping.findForward(Constants.List);
		} finally {
			if (dao != null) {
				dao.disConnect();
			}
		}
		return mapping.findForward(Constants.List);
	}

	public void initList( F0405Dao dao,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String id = CommonTools.trim(request.getParameter("id"));
			String name = CommonTools.trim(request
					.getParameter("name"));
//			供应商
			if (id.equals("supplierName")) {
				this.setFilterList(dao.filterList("pc_suppliers", "code", "supplier", "supplier like '%" + name + "%'"), response);
//			材料
			} else if (id.equals("materialName")) {
				this.setFilterList(dao.filterList("t_materials", "code", "name", "name like '%" + name + "%'"), response);
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @初始�?
	 */
	public void doINIT(F0405ActionForm actionForm) throws SelectException {
		actionForm.setCurrentPage(1);
	}

	public void initActionForm(F0405ActionForm actionForm) {
		actionForm.setMaterialCode("");
		actionForm.setMaterialName("");
		actionForm.setSupplierCode("");
		actionForm.setSupplierCode1("");
		actionForm.setSupplierName("");
		actionForm.setId("");
		actionForm.setCode("");
	}
}
